package com.ruoyi.animal.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.animal.domain.AnimalInformation;
import com.ruoyi.animal.domain.AnimalSpecies;
import com.ruoyi.animal.service.ExperimentDesignService;
import com.ruoyi.animal.service.IAnimalSpeciesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.animal.domain.AnimalInformationHistory;
import com.ruoyi.animal.service.IAnimalInformationHistoryService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 动物信息回溯Controller
 * 
 * @author gw
 * @date 2025-01-15
 */
@RestController
@RequestMapping("/history")
public class AnimalInformationHistoryController extends BaseController
{
    @Autowired
    private IAnimalInformationHistoryService animalInformationHistoryService;

    @Autowired
    private IAnimalSpeciesService animalSpeciesService;

    @Autowired
    private ExperimentDesignService experimentDesignService;

    /**
     * 查询动物信息回溯列表
     */
    @RequiresPermissions("animal:history:list")
    @GetMapping("/list")
    public TableDataInfo list(AnimalInformationHistory animalInformationHistory)
    {
        startPage();
        List<AnimalInformationHistory> list = animalInformationHistoryService.selectAnimalInformationHistoryList(animalInformationHistory);
        return getDataTable(list);
    }

    /**
     * 导出动物信息回溯列表
     */
    @RequiresPermissions("animal:history:export")
    @Log(title = "动物信息回溯", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AnimalInformationHistory animalInformationHistory)
    {
        List<AnimalInformationHistory> list = animalInformationHistoryService.selectAnimalInformationHistoryList(animalInformationHistory);
        ExcelUtil<AnimalInformationHistory> util = new ExcelUtil<AnimalInformationHistory>(AnimalInformationHistory.class);
        util.exportExcel(response, list, "动物信息回溯数据");
    }

    /**
     * 获取动物信息回溯详细信息
     */
    @RequiresPermissions("animal:history:query")
    @GetMapping(value = "/{historyId}")
    public AjaxResult getInfo(@PathVariable("historyId") Long historyId)
    {
        return success(animalInformationHistoryService.selectAnimalInformationHistoryByHistoryId(historyId));
    }

    /**
     * 新增动物信息回溯
     */
    @RequiresPermissions("animal:history:add")
    @Log(title = "动物信息回溯", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AnimalInformationHistory animalInformationHistory)
    {
        return toAjax(animalInformationHistoryService.insertAnimalInformationHistory(animalInformationHistory));
    }

    /**
     * 修改动物信息回溯
     */
    @RequiresPermissions("animal:history:edit")
    @Log(title = "动物信息回溯", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AnimalInformationHistory animalInformationHistory)
    {
        return toAjax(animalInformationHistoryService.updateAnimalInformationHistory(animalInformationHistory));
    }

    /**
     * 删除动物信息回溯
     */
    @RequiresPermissions("animal:history:remove")
    @Log(title = "动物信息回溯", businessType = BusinessType.DELETE)
	@DeleteMapping("/{historyIds}")
    public AjaxResult remove(@PathVariable Long[] historyIds)
    {
        return toAjax(animalInformationHistoryService.deleteAnimalInformationHistoryByHistoryIds(historyIds));
    }

    @GetMapping(value = "getInfoByAnimalId/{animalId}")
    public AjaxResult getInfoByAnimalId(@PathVariable("animalId") Long animalId)
    {
        List<AnimalInformationHistory> animalInformationHistories = animalInformationHistoryService.selectAnimalInformationHistoryByAnimalId(animalId);
        for(AnimalInformationHistory animalInformation:animalInformationHistories){
            AnimalSpecies animalSpecies = animalSpeciesService.selectAnimalSpeciesBySpeciesId(animalInformation.getSpeciesId());
            AnimalSpecies animalSpecies1 = animalSpeciesService.selectAnimalSpeciesBySpeciesId(animalInformation.getBreed());
            if(animalSpecies!=null && animalSpecies1!=null) {
                animalInformation.setSpeciesName(animalSpecies.getSpeciesName());
                animalInformation.setBreedName(animalSpecies1.getSpeciesName());
            }
            if(animalInformation.getDesignId()!=null) {
                AjaxResult ajaxResult = experimentDesignService.getInfo(animalInformation.getDesignId());
                if(ajaxResult!=null) {
                    Map map = (Map) ajaxResult.get("data");
                    animalInformation.setDesignName(map.get("designName").toString());
                }
            }
        }
        return success(animalInformationHistories);
    }
}
